package Pre.SetDemo;

import java.util.Comparator;
import java.util.Iterator;
import java.util.Objects;
import java.util.TreeSet;

public class TreeSetDemo {
    static TreeSet treeSet = new TreeSet(new Comparator() {
        @Override
        public int compare(Object o1, Object o2) {
            if (o1.equals(o2)){
                return -1;
            }
            TreeSetDemo t1 = (TreeSetDemo) o1;
            TreeSetDemo t2 = (TreeSetDemo) o2;
            return t1.getStare() - t2.getStare() > 0 ? 1 : -1;
        }
    });

    private String name1;
    private int stare;
    private int price;

    public TreeSetDemo(String name1, int stare, int price) {
        this.name1 = name1;
        this.stare = stare;
        this.price = price;
    }

    public String getName1() {
        return name1;
    }

    public int getStare() {
        return stare;
    }

    public int getPrice() {
        return price;
    }

    @Override
    public String toString() {
        return "TreeSetDemo{" +
                "name1='" + name1 + '\'' +
                ", stare=" + stare +
                ", price=" + price +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        TreeSetDemo that = (TreeSetDemo) o;
        return stare == that.stare && price == that.price && Objects.equals(name1, that.name1);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name1, stare, price);
    }

    public static void main(String[] args) {
        treeSet.add(new TreeSetDemo("wxy", 12, 199));
        treeSet.add(new TreeSetDemo("sdw", 15, 89));
        treeSet.add(new TreeSetDemo("csgo", 20, 98));
        treeSet.add(new TreeSetDemo("cf", 12, 96));
        System.out.println(treeSet.size());

        Iterator iterator = treeSet.descendingIterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }
}
